import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { ApplicationStateType } from './typings'
import {
  getSystemApplicationList,
  addSystemApplicationInfo,
  updateSystemApplicationInfo,
  deleteSystemApplicationInfo
} from '~/api/system/application'

const application: Module<ApplicationStateType, ApplicationStateType> = {
  namespaced: true,
  state: {
    appTypeEnum: {
      1: '内置',
      2: '租户'
    },
    statusEnum: {
      0: '启用',
      1: '停用'
    }
  },
  getters: {
    appTypeEnum: (state) => state.appTypeEnum,
    statusEnum: (state) => state.statusEnum
  },
  actions: {
    // 获取列表信息
    GetSystemApplicationList<S, T> (_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getSystemApplicationList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 添加子系统
    AddSystemApplicationInfo<S, T> (_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        addSystemApplicationInfo<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 修改子系统
    UpdateSystemApplicationInfo<S, T> (_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        updateSystemApplicationInfo<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 删除子系统
    DeleteSystemApplicationInfo<S, T> (_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        deleteSystemApplicationInfo<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    }
  }
}

export default application
